package com.example.springbootdemo.handler;

import com.example.springbootdemo.exception.BusinessErrorException;
import com.example.springbootdemo.exception.BusinessResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * 同一账号同时登录的用户数受限的处理
 * 比如某用户同时登陆的会话数，超过了系统的设置，大白话就是被顶号了，这时会由SessionInformationExpiredStrategy处理。
 * 还有，在线用户被管理员提出后，也会触发。
 * @author hh
 * @date 2022/1/28
 */
@Slf4j
@Component
public class SessionInformationExpiredHandler implements SessionInformationExpiredStrategy {

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        throw new BusinessErrorException(BusinessResponseEnum.USER_MAX_LOGIN);
    }

}
